package com.signway.javaui.slice;

import com.signway.javaui.ResourceTable;
import com.signway.javaui.utils.SwToast;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ListDialog;
import ohos.agp.window.dialog.PopupDialog;

public class DialogAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_dialog);
        
        initView();
    }

    private void initView() {
        /* 显示ToastDialog */
        Button btnToast = (Button) findComponentById(ResourceTable.Id_dialog_toast);
        /* 显示ListDialog */
        Button btnList = (Button) findComponentById(ResourceTable.Id_dialog_list);
        /* 显示PoputDialog */
        Button btnPoput = (Button) findComponentById(ResourceTable.Id_dialog_poput);

        btnToast.setClickedListener(component -> SwToast.showToast(this, "我是一个Toast，3s后我会消失", SwToast.LONG_TIME));

        btnList.setClickedListener(component -> {
            getUITaskDispatcher().asyncDispatch(() -> {
                ListDialog listDialog = new ListDialog(DialogAbilitySlice.this, ListDialog.MULTI);
                listDialog.setSingleSelectItems(new String[]{"第1个选项", "第2个选项"}, 0);
                listDialog.setMultiSelectItems(new String[]{"第1个选项", "第2个选项"}, new boolean[]{true, false} );
                listDialog.setItems(new String[]{"First Item", "Second Item"});
                listDialog.setButton(0, "取消", (iDialog, i) -> listDialog.hide());
                listDialog.setButton(1, "确认", null);
                listDialog.setListener((listContainer, component1, i, l) -> SwToast.showToast(DialogAbilitySlice.this, "单机了第" + (i + 1) + "个选项"), null, null);
                listDialog.setSize(400, 300);
                listDialog.show();
            });
        });

        btnPoput.setClickedListener(component -> {
            getUITaskDispatcher().asyncDispatch(() -> {
                PopupDialog popupDialog = new PopupDialog(DialogAbilitySlice.this, btnPoput, 600, 100);
                popupDialog.setBackColor(Color.GRAY);
                popupDialog.setHasArrow(true);/* 设置显示气泡箭头 */
                // 创建自定义DialogBox组件
                Text text = new Text(DialogAbilitySlice.this);
                text.setText("这是一个PopupDialog!");
                text.setTextSize(50);
                text.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
                text.setClickedListener(component12 -> popupDialog.hide());
                popupDialog.setCustomComponent(text).setMode(PopupDialog.ICON2).show();
            });
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
